<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <canvas id="canvas" width="400" height="400"></canvas>
    <script src="initCanvas.js"></script>
    <script>
        var img = new Image()
        img.src = 'a.jpg'
        $(img).on('load', function () {
            context.drawImage(img, 0, 0)
            

            var imgData = context.getImageData(0, 0, canvas.width(), canvas.height()),
                pixels = imgData.data
            numPixels = pixels.length

            context.clearRect(0, 0, canvas.width(), canvas.height())
            for (var i = 0; i < numPixels; i++) {
                pixels[i * 4] = 255 - pixels[i * 4]
                pixels[i * 4 + 1] = 255 - pixels[i * 4 + 1]
                pixels[i * 4 + 2] = 255 - pixels[i * 4 + 2]
            }

            context.putImageData(imgData,0,0)
            context.drawImage(img,100,0)
        })

    </script>

</body>

</html>